﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Web.Http;
using Bll;
using Common;
using CommonModel.Config;
using IBll;
using MainService.Controllers.Extend;
using ResponseModel;
using ResponseModel.Common;
using Thrift.Protocol;
using Thrift.Transport;

namespace MainService.Controllers
{
    /// <summary>
    /// Rpc控制器
    /// </summary>
    public class RpcController : BaseApiController
    {
        /// <summary>
        /// 获取HelloWorld
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel GetHelloWorld()
        {
            ResultModel resultModel = new ResultModel();

            try
            {
                TTransport transport = new TSocket(new TcpClient(RpcConfig.HostName, int.Parse(RpcConfig.Port)));
                TProtocol protocol = new TBinaryProtocol(transport);
                IRpcBll.Client client = new IRpcBll.Client(protocol);
                if (!transport.IsOpen) { transport.Open(); }

                string result = client.GetHelloWorld();
                if (!string.IsNullOrEmpty(result))
                {
                    resultModel.Pass = true;
                    resultModel.Msg = result;
                }
                else
                {
                    resultModel.Msg = "获取HelloWorld失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("报错：" + ex.Message);
                resultModel.Msg = "报错：" + ex.Message;
            }

            return resultModel;
        }
    }
}
